package com.ktjiaoyu.crm.config.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.ktjiaoyu.crm.pojo.Right;
import com.ktjiaoyu.crm.service.RightService;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * shiro配置类
 */
@Configuration
public class ShiroConfig {
    
    @Autowired
    private RightService rightService;


    @Bean(name = "shiroDialect")
    public ShiroDialect shiroDialect() {//thymeleaf 页面上使用 shiro 标签
        return new ShiroDialect();
    }

    @Bean   //装配自定义Realm
    public MyShiroRealm myShiroRealm(){

        return new MyShiroRealm();
    }

    @Bean   //装配安全管理器
    public SecurityManager defaultWebSecurityManager(){

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //注入自定义Realm
        securityManager.setRealm(myShiroRealm());

        return securityManager;
    }

    @Bean //装配过滤器
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager manager){
        //实例化shiroFilterFactoryBean
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //注入SecurityManager
        shiroFilterFactoryBean.setSecurityManager(manager);
        //权限验证：使用fileter控制资源访问
        //身份认证失败，则跳转到登录页面的配置 没有登录的用户请求需要登录的页面时自动跳转到登录页面，
        //不是必须的属性，不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面
        shiroFilterFactoryBean.setLoginUrl("/toLogin");

        //没有权限默认跳转的页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");

        //用LinkedHashMap添加拦截的uri,其中authc指定需要认证的uri，anon指定排除认证的uri
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        //配置可以匿名访问的资源：静态资源
        //anon:所有url都都可以匿名访问;
        //authc:需要认证才能进行访问;
        //user:配置记住我或认证通过可以访问；
        filterChainDefinitionMap.put("/css/**","anon");
        filterChainDefinitionMap.put("/fonts/**","anon");
        filterChainDefinitionMap.put("/images/**","anon");
        filterChainDefinitionMap.put("/js/**","anon");
        filterChainDefinitionMap.put("/localcss/**","anon");
        filterChainDefinitionMap.put("/localjs/**","anon");
        filterChainDefinitionMap.put("/login","anon");
        //filterChainDefinitionMap.put("/logout","logout");//注销过滤器，自动注销

        //配置需要特定权限才能访问的资源(URL)
        //动态授权：将权限从数据库中查询出来
        List<Right> rightList = rightService.list();
        //iter+tab键：快速遍历集合
        for (Right right : rightList) {
            //将从数据库中获取的权限，放入filterChainDefinitionMap
            //此处判断URL不为空是因为父权限URL不需要跳转页面，所以会出现空的情况
            //需要排除父权限URL
            if(right.getRightUrl()!=null && !right.getRightUrl().trim().equals("")){
                filterChainDefinitionMap.put(right.getRightUrl(),"perms["+right.getRightCode()+"]");
            }
        }
        //静态授权：包括全部需要特定权限才能访问的资源(URL)
//        filterChainDefinitionMap.put("/user/list","perms[用户列表]");
//        filterChainDefinitionMap.put("/user/add","perms[用户添加]");
//        filterChainDefinitionMap.put("/user/edit","perms[用户编辑]");
//        filterChainDefinitionMap.put("/user/del","perms[用户删除]");
        //省略其他

        //配置认证访问：其他资源(URL)必须认证通过才能访问
        filterChainDefinitionMap.put("/**","authc"); //必须放在过滤器链的最后面

        //filterChainDefinitions的配置顺序为自上而下,以最上面的为准
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

}
